<template>
  <div class="login-container">
    <div class="login-box">
      <div
        style="
          font-weight: bold;
          font-size: 24px;
          text-align: center;
          margin-bottom: 30px;
          color: #1450aa;
        ">
        欢迎注册
      </div>
      <el-form :model="data.form" ref="formRef" :rules="data.rules">
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            size="large"
            v-model="data.form.userName"
            placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            size="large"
            v-model="data.form.userPassword"
            placeholder="请输入密码"
            show-password />
        </el-form-item>
        <el-form-item>
          <el-button size="large" type="primary" style="width: 100%" @click="dialogVisible = true">
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        还没有账号？请
        <a href="/login">登录</a>
      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog title="设置头像和昵称" v-model="dialogVisible" width="30%" @close="resetForm">
      <!-- 上传图片组件 -->
      <el-upload
        action="#"
        :show-file-list="false"
        type="list-card"
        class="avatar-uploader"
        :auto-upload="false"
        :on-change="handleFileChange">
        <img v-if="data.avatar" :src="data.avatar" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
          <Plus />
        </el-icon>
      </el-upload>

      <!-- 输入昵称 -->
      <el-input v-model="data.form.name" placeholder="请输入昵称"></el-input>

      <!-- 弹窗底部操作按钮 -->
      <span
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: center; align-items: center; margin-top: 2vh">
        <el-button style="margin: 0 10px" @click="dialogVisible = false">取消</el-button>
        <el-button style="margin: 0 10px" type="primary" @click="submitForm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { User, Lock, Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import request from '@/utils/request'
import axios from 'axios'

const dialogVisible = ref(false) // 控制弹窗显示
const formRef = ref(null) // 表单引用

const data = reactive({
  form: {
    userName: '',
    userPassword: '',
    avatar: null,
    name: ''
  },
  rules: {
    userName: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 3, max: 15, message: '账号长度在 3 到 15 个字符', trigger: 'blur' }
    ],
    userPassword: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 30, message: '密码长度在 6 到 30 个字符', trigger: 'blur' }
    ]
  }
})

// 点击注册按钮的时候会触发这个方法
const register = () => {
  const formData = new FormData()
  formData.append('userName', data.form.userName)
  formData.append('userPassword', data.form.userPassword)
  formData.append('avatar', data.form.avatar)
  formData.append('name', data.form.name)
  formRef.value.validate((valid) => {
    if (valid) {
      // 调用后台的接口
      console.log('Form data:', formData) // 用于调试
      axios
        .post('http://localhost:9090/user/register', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        .then((res) => {
          if (res.data.code === '200') {
            ElMessage.success('注册成功')
            router.push('/login')
          } else {
            ElMessage.error(res.data.msg)
          }
        })
    } else {
      ElMessage.error('请填写完整的表单信息')
    }
  })
}

const handleFileChange = (file, fileList) => {
  const reader = new FileReader()
  reader.onload = (e) => {
    data.avatar = e.target.result
    data.form.avatar = file.raw
  }
  reader.readAsDataURL(file.raw)
}

const resetForm = () => {
  data.avatar = null
  data.nickname = ''
}

const submitForm = () => {
  console.log(data.form.userName)
  console.log(data.form.userPassword)
  console.log('提交的昵称:', data.form.name)
  console.log('提交的头像:', data.form.avatar)
  // 这里可以添加将数据发送到服务器的代码
  dialogVisible.value = false
  register()
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('https://bu.dusays.com/2024/12/06/675319c04fbf1.jpg') no-repeat center center;
  background-size: cover;
}

.login-box {
  width: 350px;
  padding: 50px 30px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.5);
}

.avatar-uploader {
  width: 10vh;
  height: 10vh;
  display: flex;
  margin: auto;
  border: 1px dashed #d9d9d9;
  transition: border-color 0.3s;
  align-items: center;
  justify-content: center;
  margin-bottom: 2vh;
}

.avatar-uploader:hover {
  border-color: #409eff;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-uploader .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}

.avatar {
  width: 10vh;
  height: 10vh;
  object-fit: cover;
}
</style>
